<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>新用户注册</title>
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>

    <link rel="stylesheet" href="../js/libs/sui/sm.min.css">
    <link rel="stylesheet" href="../js/libs/sui/sm-extend.min.css">
    <link rel="stylesheet" href="../iconfont/iconfont.css">
    <link rel="stylesheet" href="../less/main.css"/>

    <script type='text/javascript' src='../js/libs/jquery-3.1.0.min.js' charset='utf-8'></script>
    <script type="text/javascript">
        var Zepto = jQuery;
    </script>
    <script type='text/javascript' src='../js/libs/sui/sm.js' charset='utf-8'></script>
    <script type='text/javascript' src='../js/libs/sui/sm-extend.min.js' charset='utf-8'></script>
    <script src="../js/libs/juicer-min.js"></script>
    <script src="../js/libs/jquery.md5.js"></script>
    <script src="../js/api.js"></script>
    <script src="../js/utils.js"></script>
    <script src="../js/common.js"></script>
</head>
<body>
<div class="content user-login bg-default p-t22">
    <!--头部-->
    <div class="head-no-toolbar bg-self-green no-left-icon">
        <span class="icon iconfont c-white p-l" onclick="javascript:api.closeWin();">&#xe61f;</span>
        <span class="c-white t-center">新用户注册</span>
    </div>
    <div class="list-block">
        <ul>
            <li>
                <div class="item-content">
                    <div class="item-media"><i class="icon iconfont">&#xe619;</i></div>
                    <div class="item-inner">
                        <div class="item-input">
                            <input id="r-user-name" type="text" placeholder="请输入手机号">
                        </div>
                    </div>
                </div>
            </li>
            <li>
                <div class="item-content">
                    <div class="item-media"><i class="icon iconfont">&#xe61e;</i></div>
                    <div class="item-inner">
                        <div class="item-input">
                            <input id="r-first-pwd" type="password" placeholder="请输入密码">
                        </div>
                    </div>
                </div>
            </li>
            <li>
                <div class="item-content">
                    <div class="item-media"><i class="icon iconfont">&#xe61e;</i></div>
                    <div class="item-inner">
                        <div class="item-input">
                            <input id="r-second-pwd" type="password" placeholder="请再次输入一次密码">
                        </div>
                    </div>
                </div>
            </li>
        </ul>
    </div>
    <div class="content-block">
        <div class="row">
            <div class="col-100"><a id="register-btn" class="button button-big button-round button-fill bg-self-green">注册</a></div>
        </div>
    </div>

</div>

<script>
    apiready=function(){
        juicer.set({
            'tag::operationOpen': '{@',
            'tag::operationClose': '}',
            'tag::interpolateOpen': '&{',
            'tag::interpolateClose': '}',
            'tag::noneencodeOpen': '$${',
            'tag::noneencodeClose': '}',
            'tag::commentOpen': '{#',
            'tag::commentClose': '}',
            'strip': false,
            'cache': false
        });

        $("#register-btn").click(function(){
            register();
        });




        function register(){
            var username=$("#r-user-name").val();
            var firstPwd=$("#r-first-pwd").val()
            var secondPwd=$("#r-second-pwd").val()

            if(getstrlength(username)==0){
                toast("请输入手机号");
                return;
            }

            if(!checkPhoneNumber(username)){
                toast("手机号格式不正确");
                return;
            }

            if(getstrlength(firstPwd)==0||getstrlength(secondPwd)==0){
                toast("请输入密码");
                return;
            }

            if(!checkPassword(firstPwd)||!checkPassword(secondPwd)){
                toast("密码由6-20位数字和字母组成");
                return;
            }


            if(firstPwd!=secondPwd){
                toast("密码不一致");
                return;
            }

            var success=0;
            //验证手机号是否存在
            var existSql="select * from su_staff where STAFF_NO='{0}'";
            dbQuery(1,1,existSql.format(username),true,function(ret,err){
                if(isObject(ret)&&ret.statusCode==0){
                    if(ret.result.length>0){
                        toast("手机号已存在，请重新输入");
                    }else{

                        //密码加密
                        var md5Pwd= $.md5(firstPwd);
                        var staffid=getUUID();

                        //插入数据su_staff
                        var staffsql="INSERT INTO su_staff(STAFF_ID,STAFF_NO,PWD,STAFF_TYPE,STATE,SYS_FLAG,CREATE_DATE,MOBILE_PHONE) VALUES('"+staffid+"','"+username+"','"+md5Pwd+"','1','A','0',NOW(),'"+username+"')";
                        //插入数据s_student
                        var studentsql="INSERT INTO s_student(STAFF_ID,MOBILE_PHONE,CREATE_DATE) VALUES('"+staffid+"','"+username+"',NOW())";

                        dbExecute(staffsql,true,function(ret,err){

                            if(ret.statusCode==0){
                                success+=1;
                                regSuccess()
                            }

                        })
                        dbExecute(studentsql,true,function(ret,err){
                            if(ret.statusCode==0){
                                success+=1;
                                regSuccess()
                            }
                        })
                    }
                }else{
                    toast("数据出错，请重试");
                }
            });
            function regSuccess(){
                if(success==2){
                    toast("注册成功！");

                    //发送登陆成功事件
                    api.sendEvent({
                        name: 'regSuccessEvent',
                        extra: {phone: username}
                    });
                    closeWin();
                }
            }
        }
    };
</script>
</body>
</html>